/*
 * @(#)SharedPreferencesUtil.java 1.0 2013/9/2
 * Copyright 2013 PD, Inc. All rights reserved.
 * 
 */
package cn.broadin.app.hotel.util;

import java.util.Map;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

/**
 * 
 * SharedPreferencesUtil SharedPreferences工具类
 * @author fenghui
 *
 */
public class SharedPreferencesUtil {
	private static final String TAG = "SharedPreferencesUtil";
	SharedPreferences sharedPrefrences = null;			//文件存储
	
	/**
	 * 配置文件读写对象
	 * @param context
	 */
	public SharedPreferencesUtil(Context context) {
		Log.i(TAG, "get SharedPreferences file.");
		sharedPrefrences = context.getSharedPreferences(
				"pd_ott_config", Context.MODE_PRIVATE);
	}
	
	/**
	 * 从配置文件中获取值
	 * @param key 要获取的key
	 * @return key对应的值
	 */
	public String getValue(String key) {
		Log.i(TAG, "get SharedPreferences, key is '" + key + "'");
		if(sharedPrefrences == null) { 
			return "";
		} else {
			return sharedPrefrences.getString(key, "");
		}
	}
	
	/**
	 * 向配置文件中写入键值对格式数据
	 * @param saveItems	map格式数据
	 * @return
	 */
	public boolean setValues(Map<String, String> saveItems) {
		if(sharedPrefrences == null) { 
			return false;
		} else {
			try {
				Editor editor = sharedPrefrences.edit();
				for(String keyItem : saveItems.keySet()){
					String itemVal = saveItems.get(keyItem);
					Log.i(TAG, "set SharedPreferences, key is '" + keyItem + "', value is '" + itemVal + "'");
					editor.putString(keyItem, saveItems.get(keyItem));
				}
				editor.commit();
				return true;
			} catch (Exception e) {
				Log.e(TAG, "set SharedPreferences failed!!!");
				e.printStackTrace();
			}
			return false;
		}
	}
}